package wangjg.security.deprecated;

import java.util.ArrayList;
import java.util.Set;

import wangjg.security.core.AuthoritySet;
import wangjg.security.http.HttpRequest;
import wangjg.security.util.matcher.Wildcard;

/**
 * http资源，可以是一个或一组url
 * @author wangjigang1
 *
 */
public class HttpResource implements wangjg.security.http.HttpResource, AuthoritySet, Resource{
	private ArrayList<ResourceValidator> validators;
	protected String pattern;
	private Set authorities;

	public HttpResource() {
		super();
	}

	/**
	 * 是否请求的当前资源
	 */
	public boolean match(Object request) {
		if(request instanceof HttpRequest){
			HttpRequest httpRequest=(HttpRequest)request;
			return Wildcard.matches(this.pattern, httpRequest.getPath());	
		}
		return false;
	}
	
	/**
	 * 指定的用户是否有权限访问当前资源
	 * @param user
	 * @return
	 */
	public boolean validate(Object user){
		for(ResourceValidator validator : validators){
			if(validator.validate(this,user)){
				return true;
			}
		}
		return false;
	}

	public ArrayList<ResourceValidator> getValidators() {
		return validators;
	}

	public void setValidators(ArrayList<ResourceValidator> validators) {
		this.validators = validators;
	}

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	public Set getAuthorities() {
		return authorities;
	}

	public void setAuthorities(Set authorities) {
		this.authorities = authorities;
	}
}
